﻿using System;
using System.Data;
using System.Configuration;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
namespace BLL
{
    /// <summary>
    /// Summary description for MenuManager
    /// </summary>
    public class MenuManager
    {
        public MenuManager()
        {
            //
            // TODO: Add constructor logic here
            //
        }

        public XElement BuildXMLMenu(string setCategoryMethod)
        {
            using (DAL.EShopDBDataContext db = new DAL.EShopDBDataContext())
            {
                var cat = from c in db.Categories
                          where c.Parent == 1 orderby c.PriorityOrder
                          select c;
                db.Refresh(System.Data.Linq.RefreshMode.OverwriteCurrentValues, cat);
                XElement menu = new XElement("Menu", new XAttribute("text", Resources.Notifications.ChooseCategory), new XAttribute("url", string.Format("javascript:alert('{0}')", Resources.Notifications.ChooseCategory)));
                BuildXmlTree(menu, cat, setCategoryMethod);
                return menu;
            }
        }

        private XElement BuildXmlTree(XElement menu, IQueryable<DAL.Category> cat, string setCategoryMethod)
        {
            foreach (DAL.Category ct in cat)
            {
                menu.Add(
                    BuildXmlTree(
                    new XElement("Submenu",
                        new XAttribute("url", string.Format("javascript:{2}({0}, '{1}');", ct.CategoryId, ct.Title, setCategoryMethod)),
                        new XAttribute("text", ct.Title)), ct.Categories.AsQueryable(), setCategoryMethod));
            }
            return menu;
        }
    }
}